/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentacion.dialogos;

import conexion.Conexion;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.SwingWorker;
import presentacion.comun.FramePortDB;
import presentacion.comun.GlassPanel;

/**
 *
 * @author Luis
 */
public class Paso2 extends javax.swing.JDialog {

    private Conexion sqlCon = new Conexion();
    private ConectarAction conectaraction = new ConectarAction();
    private CancelarAction cancelAction = new CancelarAction();
    private SiguienteAction siguienteAction = new SiguienteAction();
    private  List<List<String>> lista = new ArrayList<List<String>>();
    private FramePortDB parent;
    
    public Paso2(FramePortDB parent, boolean modal, List<List<String>> lista) {
        super(parent, modal);
        initComponents();
        this.setGlassPane(new GlassPanel(false));
        this.pnlDataBase.setVisible(false);
        this.pack();
        this.lista = lista;
        this.parent=parent;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        pnlDataBase = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        cboBasesDatos = new javax.swing.JComboBox();
        pnlDatos = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        pgbProgreso = new javax.swing.JProgressBar();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel6.setText("Seleccione la Base de datos a la que se desea Conectar");

        org.jdesktop.layout.GroupLayout pnlDataBaseLayout = new org.jdesktop.layout.GroupLayout(pnlDataBase);
        pnlDataBase.setLayout(pnlDataBaseLayout);
        pnlDataBaseLayout.setHorizontalGroup(
            pnlDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDataBaseLayout.createSequentialGroup()
                .add(27, 27, 27)
                .add(pnlDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(pnlDataBaseLayout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(cboBasesDatos, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(jLabel6))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        pnlDataBaseLayout.setVerticalGroup(
            pnlDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDataBaseLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(cboBasesDatos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setText("Conexion SQL Server");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${sqlCon.servidor}"), jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel4.setText("Contraseña:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${sqlCon.user}"), jTextField2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel3.setText("Usuario:");

        jLabel2.setText("Servidor:");

        jButton1.setAction(conectaraction);
        jButton1.setText("Conectar");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${sqlCon.password}"), jPasswordField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${sqlCon.puerto}"), jTextField3, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel5.setText("Puerto:");

        org.jdesktop.layout.GroupLayout pnlDatosLayout = new org.jdesktop.layout.GroupLayout(pnlDatos);
        pnlDatos.setLayout(pnlDatosLayout);
        pnlDatosLayout.setHorizontalGroup(
            pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlDatosLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel1)
                .add(124, 124, 124))
            .add(pnlDatosLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jButton1)
                    .add(pnlDatosLayout.createSequentialGroup()
                        .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel3)
                            .add(jLabel2)
                            .add(jLabel4)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jTextField1)
                            .add(jTextField2)
                            .add(jPasswordField1)
                            .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 266, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlDatosLayout.setVerticalGroup(
            pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(18, 18, 18)
                .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(9, 9, 9)
                .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlDatosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pgbProgreso.setStringPainted(true);

        jButton2.setAction(siguienteAction);
        jButton2.setText("Siguiente");

        jButton3.setAction(cancelAction);
        jButton3.setText("Anterior");

        jButton4.setAction(cancelAction);
        jButton4.setText("Cancelar");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlDataBase, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(pnlDatos, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(pgbProgreso, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButton3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(pnlDatos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlDataBase, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton3)
                    .add(jButton4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(pgbProgreso, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Paso2 dialog = new Paso2(new FramePortDB(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cboBasesDatos;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JProgressBar pgbProgreso;
    private javax.swing.JPanel pnlDataBase;
    private javax.swing.JPanel pnlDatos;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the sqlCon
     */
    public Conexion getSqlCon() {
        return sqlCon;
    }

    /**
     * @param sqlCon the sqlCon to set
     */
    public void setSqlCon(Conexion sqlCon) {
        this.sqlCon = sqlCon;
    }

    private class ConectarAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent ae) {
            pgbProgreso.setString("Conectando....");
            pgbProgreso.setIndeterminate(true);
            getGlassPane().setVisible(true);
            SwingWorker swingWorker = new SwingWorker() {
                @Override
                protected Object doInBackground() throws Exception {
                    sqlCon.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    try {
                        Connection con = sqlCon.getConexionSqlServer();

                        if (con != null) {
                            
                            for (Component object : pnlDatos.getComponents()) {
                                object.setEnabled(false);
                            }
                            cboBasesDatos.setEnabled(false);
                            pnlDataBase.setVisible(true);
                            
                            for (String object : sqlCon.getMetaData()) {
                                cboBasesDatos.addItem(object);
                            } 
                        }

                    } catch (SQLException ex) {
                        pgbProgreso.setIndeterminate(false);
                        getGlassPane().setVisible(false);
                        pgbProgreso.setString("Error al Conectar al Servidor.");
                        System.out.println("error:"+ex);
                        return null;
                    }
                    cboBasesDatos.setEnabled(true);
                    pgbProgreso.setIndeterminate(false);
                    getGlassPane().setVisible(false);
                    pgbProgreso.setString("Conexión al Servidor Datos Correcta.");
                    return null;
                }
            };
            swingWorker.execute();

        }
    }
    
    private class CancelarAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent ae) {
            setVisible(false);
        }
        
    
    }
    
    private class SiguienteAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent ae) {
            sqlCon.setDb(""+cboBasesDatos.getSelectedItem().toString());
            try {
                Paso3 pasito3 = new Paso3(parent, true, lista, sqlCon.getConexionSqlServer());
                pasito3.setVisible(true);
            } catch (SQLException ex) {
               pgbProgreso.setString(ex.getMessage());
            }
                    
        }
    
    
    }
}
